package com.goscom.app.deeset;

import com.goscom.app.database.DBAdapter;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class ContactEmergency extends Activity {

	private String strMerId = "";
	private DBAdapter mDB;
	private EditText edtAddTelNum;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contactemergency);

		edtAddTelNum = (EditText) findViewById(R.id.edtConEmerAddTelNum);

		mDB = new DBAdapter(ContactEmergency.this);
		mDB.open();

		getMerId();
	}

	public void getMerId() {
		try {
			strMerId = getIntent().getStringExtra("merid");
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (strMerId != null) {
			ContentValues content = new ContentValues();
			content = mDB.queryDataByMerId("CONEMER", strMerId);
			if (content != null) {
				edtAddTelNum.setText(content.getAsString("addtelnum"));
			}
		}
	}

	public void Logout(View v) {
		saveDB();
		if (mDB != null) {
			mDB.close();
		}
		startActivity(new Intent(ContactEmergency.this, Login.class));
	}

	public void goPrevious(View v) {
		saveDB();
		if (mDB != null) {
			mDB.close();
		}
		Intent intent = new Intent(ContactEmergency.this, Referee.class);
		intent.putExtra("merid", strMerId);
		intent.putExtra("activity", "referee2");
		startActivity(intent);
	}

	public void goNext(View v) {
		saveDB();
		if (mDB != null) {
			mDB.close();
		}
		startActivity(new Intent(ContactEmergency.this, Rehabilitation.class)
				.putExtra("merid", strMerId));
	}

	public void Save(View v) {
		saveDB();
		Toast.makeText(ContactEmergency.this, "Save Successful!",
				Toast.LENGTH_LONG).show();
	}

	public void saveDB() {
		if (mDB.queryMerchandiser("CONEMER", strMerId) == 0) {
			insertDB();
		} else {
			mDB.deleteData("CONEMER", strMerId);
			insertDB();
		}
	}

	public void insertDB() {
		ContentValues content = new ContentValues();
		content.put("merid", strMerId);
		content.put("addtelnum", edtAddTelNum.getText().toString());
		mDB.insertData("CONEMER", content);
	}

	@Override
	protected void onRestart() {
		mDB.open();
		super.onRestart();
	}

	@Override
	protected void onDestroy() {
		if (mDB != null) {
			mDB.close();
		}
		super.onDestroy();
	}
}
